
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户组管理</title>
    {include file="include/top"}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">组名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" value="" placeholder="支持模糊搜索"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-header layuiadmin-card-header-auto">
            <button class="layui-btn layuiadmin-btn-tags" data-type="add">添加</button>
        </div>
        <table class="layui-table" id="demo" lay-data="{height:630, url:'/adminauth/userGroupList', page:true, id:'idTest' ,cellMinWidth: 80}" lay-filter="demo">
            <thead>
            <tr>
                <th lay-data="{type:'checkbox'}"></th>
                <th lay-data="{field:'id',  sort: true}">ID</th>
                <th lay-data="{field:'title'}">名称</th>
                <th lay-data="{ align:'center', toolbar: '#barDemo'}"></th>
            </tr>
            </thead>
        </table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>
{include file="include/js"}

<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table','form'], function(){
        var table = layui.table;
        var form = layui.form;
        var id=0;
        var $ = layui.$, active = {
            add: function(){
                id=0
                addGroup()
            }
        }
        function addGroup(){
            layer.open({
                type: 2
                ,title: '添加修改组'
                ,content: 'userGroupAdd?id='+id
                ,area: ['450px', '600px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                            ,title = othis.find('input[name="title"]').val();
                    $.post("/adminauth/userGroupSave",{id:id,title:title},function (re) {
                        if(re.code==0){
                            layer.msg(re.msg);
                            setTimeout(function () {
                                location.reload();
                            },2000);
                        }else{
                            layer.msg(re.msg);
                        }
                    },'json');
                }
            });
        }
        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('idTest', {
                where: field
            });
        });



        $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.layui-btn.getCheckData').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //监听工具条
        table.on('tool(demo)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){

            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                id=data.id
                addGroup()
            }
        });
        table.render({
            elem: '#test'
            ,url:'/adminauth/authgrouplist'
            ,cols: [[
                {field:'id', width:80, title: 'ID', sort: true}
                ,{field:'title', width:280, title: '名称'}
            ]]
            ,page: true
        });
    });
</script>

</body>
</html>
